package xyc.sjxy.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.PropertiesUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils {
    private static String driver ;
    private static String url;
    private static String username;
    private static String password;

    private static DataSource dataSource;
    static {
     /*   InputStream inputStream = JdbcUtils.class.getClassLoader().
                getResourceAsStream("jdbc.properties");*/
        Properties properties = PropertiesUtils.loadProperties("/jdbc.properties");
        driver = properties.getProperty("jdbc.driver");
        url = properties.getProperty("jdbc.url");
        username = properties.getProperty("jdbc.username");
        password = properties.getProperty("jdbc.password");
    }
    public static DataSource getDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        dataSource = druidDataSource;
        return druidDataSource;
    }
    public static Connection getConnection() throws SQLException, SQLException {
        return dataSource.getConnection();
    }
}
